/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lunify;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;

/**
 *
 * @author katie
 */
public class Rows {

    private HashSet<Row> rows;

    public Rows() {
        rows = new HashSet<Row>();
    }

    public void add(Collection<Row> rows) {
        this.rows.addAll(rows);
    }

    public void add(Rows rows, int... order) {
        for (Row r : rows.rows) {
            add(r, order);
        }
    }

    public boolean add(Row row, int... relevant) {
        for (Row r : rows) {
            if (r.compareTo(row, relevant) == 0) {
                return false;
            }
        }
        return add(row);
    }

    public boolean add(Row row) {
        return this.rows.add(row);
    }

    public int haveWhiteSpace(int... relevant) {
        int out = 0;
        for (Row r : rows) {
            if (r.hasWhiteSpace(relevant)) {
                out++;
            }
        }
        return out;
    }

    public String toSortedString(boolean filterOutWWhite, boolean remUniques, String base, String replace, int... order) {
        return toString(filterOutWWhite, sortedBy(remUniques?order:new int[0]), base, replace, order);
    }

    public static String toString(boolean filterOutWWhite, ArrayList<Row> list, String base, String replace, int... order) {
        StringBuilder sb = new StringBuilder();
        for (Row r : list) {
            if (!r.hasWhiteSpace(order)) {
                sb.append(r.toString(base, replace, order)).append("\n");
            }
        }
        return sb.toString();
    }

    public ArrayList<Row> sortedBy(final int... order) {
        ArrayList lrows = new ArrayList<Row>();
        lrows.addAll(rows);
        if (order.length > 0) {
            Comparator<Row> cr = new Comparator<Row>() {

                @Override
                public int compare(Row o1, Row o2) {
                    return o1.compareTo(o2, order);
                }
            };
            Collections.sort(lrows, cr);
        }
        return lrows;
    }

    public String toString(int... relevant) {
        if (relevant.length > 0) {
            int wsp = haveWhiteSpace(relevant);
            return "no whitespace / total = " + wsp + " / " + rows.size() + " = " + ((double) wsp/(double) rows.size());
        } else {
            return toString();
        }
    }

    @Override
    public String toString() {
        return "total=" + rows.size();
    }
}
